/**
 * @projectName: project02bywdmtz
 * @package: com.cskaoyan.service
 * @className: WxFootprintServiceImpl
 * @author: 冉夏
 * @description: TODO
 * @date: 2023/1/2 17:06
 * @version: 1.8
 */

package com.cskaoyan.service;

import com.cskaoyan.bean.MarketFootprint;
import com.cskaoyan.bean.MarketFootprintExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxFootHisInfoVo;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class WxFootprintServiceImpl implements WxFootprintService{

    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    /*
    * 历史足迹的全部显示
    * */

    @Override
    public CommonData<WxFootHisInfoVo> getAllFootPrintInfo(BasePageInfo basePageInfo) {
        //设置页面规则
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        //找到当前用户的id
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        //查出历史足迹表中的所有信息,以及新添加的价格属性
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        marketFootprintExample.setOrderByClause(new MarketFootprint().getAddTime()+" "+"desc");
        marketFootprintExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(principal.getId());
        //查出所有的历史足迹信息
        List<MarketFootprint> footprints = footprintMapper.selectByExample(marketFootprintExample);
        //建立需要返回的集合
        List<WxFootHisInfoVo> wxFootHisInfoList = new LinkedList<>();
        //查出历史足迹下该商品的价格
        for (MarketFootprint footprint : footprints) {
            //建立返回集合中的对象
            WxFootHisInfoVo wxFootHisInfoVo = new WxFootHisInfoVo();
            //获得当前足迹对应的商品的详情
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            wxFootHisInfoVo.setAddTime(footprint.getAddTime());
            wxFootHisInfoVo.setGoodsId(footprint.getGoodsId());
            wxFootHisInfoVo.setBrief(marketGoods.getBrief());
            wxFootHisInfoVo.setName(marketGoods.getName());
            wxFootHisInfoVo.setPicUrl(marketGoods.getPicUrl());
            wxFootHisInfoVo.setRetailPrice(marketGoods.getRetailPrice());
            wxFootHisInfoVo.setId(footprint.getId());
            wxFootHisInfoList.add(wxFootHisInfoVo);
        }

        //获取分页信息
        PageInfo<WxFootHisInfoVo> footprintPageInfo = new PageInfo<WxFootHisInfoVo>(wxFootHisInfoList);
        int pages = footprintPageInfo.getPages();
        int pageNum = footprintPageInfo.getPageNum();
        long total = footprintPageInfo.getTotal();

        //给返回的commomdata赋值
        CommonData<WxFootHisInfoVo> footprintCommonData = new CommonData<>();
        footprintCommonData.setList(wxFootHisInfoList);
        footprintCommonData.setLimit(basePageInfo.getLimit());
        footprintCommonData.setPage(basePageInfo.getPage());
        footprintCommonData.setTotal((int)total);
        footprintCommonData.setPages(pages);
        return footprintCommonData;
    }
/*
* 根据传来的id删除历史足迹
* */
    @Override
    public void deleteFootprintInfo(Integer id) {
        //根据id修改装改码
        MarketFootprint marketFootprint = new MarketFootprint();
        //修改更新时间
        Date updateTime = new Date(System.currentTimeMillis());
        marketFootprint.setId(id);
        marketFootprint.setUpdateTime(updateTime);
        marketFootprint.setDeleted(true);
        footprintMapper.updateByPrimaryKeySelective(marketFootprint);
    }
}
